﻿Public Class XFormEvento 

    Private Sub XFormEvento_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        CargarDatos()
    End Sub

    Sub CargarDatos()
        Using DtEvento As DataTable = EventosTableAdapter.GetDataByEstado(True)
            If DtEvento.Rows.Count = 1 Then
                EventosTableAdapter.FillByEstado(DsEvento.Eventos, True)
                MdiParent.Text = String.Format("{0} {1}", DtEvento(0)(1), DtEvento(0)(2))
                EventoActivo.IDEvento = DtEvento(0)(0)
                EventoActivo.Anio = DtEvento(0)(2)

                BloquearControles(True)
                btnOK.Text = "Finalizar evento"
            Else
                'EventosBindingSource.AddNew()
                BloquearControles(False)
                AnioTextEdit.EditValue = Microsoft.VisualBasic.Year(Now)
                DescripcionTextEdit.Focus()
                btnOK.Text = "Comenzar nuevo evento"
            End If
        End Using
    End Sub

    Sub BloquearControles(ByVal TF As Boolean)
        DescripcionTextEdit.Properties.ReadOnly = TF
        AnioTextEdit.Properties.ReadOnly = TF
    End Sub

    Private Sub btnOK_Click(sender As System.Object, e As System.EventArgs) Handles btnOK.Click
        If btnOK.Text = "Finalizar evento" Then
            If MsgBox("Está seguro de finalizar el evento?", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "Confirme") = vbYes Then
                EventosTableAdapter.UpdateEstado(False, EventoActivo.IDEvento)
                MdiParent.Text = "No hay evento activo"
                CargarDatos()
                EventoActivo.IDEvento = 0
                Try
                    XFormParticipantes.Close()
                    XFormJueces.Close()
                Catch ex As Exception
                End Try
            End If
        ElseIf btnOK.Text = "Comenzar nuevo evento" Then
            EventosTableAdapter.Insert(DescripcionTextEdit.EditValue, AnioTextEdit.EditValue, True)
            CargarDatos()
            Try
                XFormParticipantes.Close()
                XFormJueces.Close()
            Catch ex As Exception
            End Try
        End If
    End Sub
End Class